package tools;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.beans.*;

/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:
 * @author
 * @version 1.0
 */

public class WebQueryWorkbenchFrame extends JFrame {

  private JMenuBar menuBar = new JMenuBar();
  private JMenu fileMenu = new JMenu( "File" );
  private JMenu editMenu = new JMenu( "Edit" );

  private Map<String,Action> actions = new HashMap<String,Action>();
  private ApplicationConfiguration appConfig;

  public WebQueryWorkbenchFrame() {
    super( "HTML Extractor Workbench" );

    appConfig = ApplicationConfiguration.getInstance( this );

    menuBar.add( fileMenu );
    menuBar.add( editMenu );

    addMenuItem( fileMenu, "open", new OpenAction() );
    addMenuItem( fileMenu, "save", new SaveAction() );
    addMenuItem( fileMenu, "saveas", new SaveAsAction() );
    fileMenu.addSeparator();
    addMenuItem( fileMenu, "exit", new ExitAction() );

    addMenuItem( editMenu, "undo", "Undo" );
    addMenuItem( editMenu, "redo", "Redo" );
    editMenu.addSeparator();
    addMenuItem( editMenu, "copy", "Copy" );
    addMenuItem( editMenu, "cut", "Cut" );
    addMenuItem( editMenu, "paste", "Paste" );

    setJMenuBar( menuBar );

    setDefaultCloseOperation( WindowConstants.DO_NOTHING_ON_CLOSE );

    addWindowListener( new WindowAdapter(){
      public void windowClosing(WindowEvent e){
        ActionEvent ae = new ActionEvent( this, ActionEvent.ACTION_PERFORMED, "Exit" );

        invokeAction( "exit", ae );
      }
    } );
    WebQueryWorkbench wb = new WebQueryWorkbench();
    wb.addPropertyChangeListener( "actions", new PropertyChangeListener(){
      public void propertyChange( PropertyChangeEvent evt ){
        Object newVal = evt.getNewValue();
        Object oldVal = evt.getOldValue();
        Map actionsMap = null;
        boolean disassociate = false;
//System.out.println( "actions: " + newVal );

        if ( newVal instanceof Map || oldVal instanceof Map){
          if ( newVal == null ) {
            if ( oldVal != null ){
              actionsMap = (Map)oldVal;
              disassociate = true;
            }
          } else {
            actionsMap = (Map)newVal;
          }

          if ( actionsMap != null ){
            Set keys = actionsMap.keySet();
            Iterator iter = keys.iterator();
            while ( iter.hasNext() ){
              Object key = iter.next();
              Action a = (Action)actions.get( key );

              if ( a instanceof DynamicAction ){
                DynamicAction da = (DynamicAction)a;

                da.setAction( disassociate ? null : (Action)actionsMap.get( key ) );
              }
            }
          }
        }
      }
    } );

    getContentPane().add( new JPanel(), BorderLayout.EAST );
    getContentPane().add( new JPanel(), BorderLayout.WEST );
    getContentPane().add( new JPanel(), BorderLayout.NORTH );
    getContentPane().add( new JPanel(), BorderLayout.SOUTH );
    getContentPane().add( wb, BorderLayout.CENTER );
    pack();
  }

  protected void addMenuItem( JMenu menu, String actionName, String menuItemText ){
    AbstractAction a = new DynamicAction( menuItemText );

    addMenuItem( menu, actionName, a );
  }

  protected void addMenuItem( JMenu menu, String actionName, Action a ){
    actions.put( actionName, a );

    Icon icon = appConfig.getIcon( actionName );
    if ( icon != null ){
      a.putValue( a.SMALL_ICON, icon );
    }

    ActionMenuItem mItem = new ActionMenuItem( actionName, a );
    menu.add( mItem );
  }

  protected void invokeAction( String actionName, ActionEvent evt ){
    Object obj = actions.get( actionName );

    if ( obj != null && obj instanceof Action ){
      Action a = (Action)obj;

      a.actionPerformed( evt );
    }
  }

  public class ExitAction extends AbstractAction {
    public ExitAction(){
      super( "Exit" );

      putValue( SHORT_DESCRIPTION, "Exit Application" );
      putValue( MNEMONIC_KEY, new Integer( 'x' ) );
      putValue( ACCELERATOR_KEY,
                  KeyStroke.getKeyStroke( KeyEvent.VK_F4, KeyEvent.ALT_MASK, false ) );
    }

    public void actionPerformed( ActionEvent e ){
      System.out.println( getValue( SHORT_DESCRIPTION ) );
      System.exit( 0 );
    }
  }

  private class OpenAction extends DynamicAction {
    public OpenAction(){
      super( "Open" );

      putValue( MNEMONIC_KEY, new Integer( 'O' ) );
      putValue( ACCELERATOR_KEY, KeyStroke.getKeyStroke( 'O', KeyEvent.CTRL_MASK ) );
    }
  }

  private class SaveAction extends DynamicAction {
    public SaveAction(){
      super( "Save" );

      putValue( MNEMONIC_KEY, new Integer( 'S' ) );
      putValue( ACCELERATOR_KEY, KeyStroke.getKeyStroke( 'S', KeyEvent.CTRL_MASK ) );
    }
  }

  private class SaveAsAction extends DynamicAction {
    public SaveAsAction(){
      super( "Save As..." );

      putValue( MNEMONIC_KEY, new Integer( 'v' ) );
    }
  }

  static public void main( String[] args ){
    WebQueryWorkbenchFrame f = new WebQueryWorkbenchFrame();

    f.setVisible( true );
  }
}